home *** CD-ROM | disk | FTP | other *** search
/ Aminet 8 / Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso / Aminet / dev / gcc / libnixV1_0.lha / gnu / SetMathPatch.lha / SetMathPatch.c < prev    next >
C/C++ Source or Header  |  1995-07-06  |  6KB  |  244 lines

  1. /*********************************
  2. **********************************
  3. **                              **
  4. **     $File: SetMthPatch.c$    **
  5. **     $Datum: 06.07.95   $     **
  6. **     $Version: 1.00     $     **
  7. **                              **
  8. **         Written 1995         **
  9. **       by Andreas Wolff       **
  10. **                              **
  11. **********************************
  12. *********************************/
  13.  
  14.  
  15. char *dummy="$VER: SetMathPatch 1.00 (6.7.95)";
  16.  
  17.  
  18. #include "exec/execbase.h"
  19. #include "exec/types.h"
  20. #include "exec/semaphores.h"
  21. #include "exec/memory.h"
  22. #include "dos/dos.h"
  23.  
  24. #include "string.h"
  25.  
  26.  
  27. #define SEMANAME "SetMathPatch"
  28. #define PATCHSIZE (ULONG)PatchEnd-(ULONG)PatchBegin
  29.  
  30.  
  31. extern struct ExecBase *SysBase;
  32. struct MathIEEEBase *MathIeeeSingBasBase = NULL;
  33. struct MathIEEEBase *MathIeeeDoubBasBase = NULL;
  34.  
  35. struct sema_struct { 
  36.               struct SignalSemaphore sm_sema;
  37.               char                   sm_name[sizeof(SEMANAME)+1];
  38.             };
  39.  
  40. struct sema_struct *sema=NULL;
  41. int __nocommandline;
  42.  
  43.  
  44. float LVOIEEESPFix(float, float);
  45. float LVOIEEESPMul(float, float);
  46. float LVOIEEESPDiv(float, float);
  47. double LVOIEEEDPFix(double,double);
  48. void PatchBegin(void);
  49. float NewIEEESPFix(float, float);
  50. float NewIEEESPMul(float, float);
  51. float NewIEEESPDiv(float, float);
  52. double NewIEEEDPFix(double,double);
  53. void PatchEnd(void);
  54.  
  55. asm("
  56. .globl _PatchBegin
  57. _PatchBegin:
  58. _NewIEEESPFix:
  59.     fmoves    d0,fp0
  60.     fmoveml fpcr,d1
  61.     moveq #16,d0
  62.     orl d1,d0
  63.     andw #-33,d0
  64.     fmoveml d0,fpcr
  65.     fmovel fp0,d0
  66.     fmoveml d1,fpcr
  67.         rts
  68.  
  69. _NewIEEESPMul:
  70.     fmoves    d0,fp0
  71.     fmoves    d1,fp1
  72.     fmulx    fp1,fp0
  73.     fmoves    fp0,d0
  74.     rts
  75.  
  76. _NewIEEESPDiv:
  77.     fmoves    d0,fp0
  78.     fmoves    d1,fp1
  79.     fdivx    fp1,fp0
  80.     fmoves    fp0,d0
  81.     rts
  82.  
  83. _NewIEEEDPFix:
  84.         movel d1,sp@-
  85.         movel d0,sp@-
  86.         fmoved sp@+,fp0
  87.     fmoveml fpcr,d1
  88.     moveq #16,d0
  89.     orl d1,d0
  90.     andw #-33,d0
  91.     fmoveml d0,fpcr
  92.     fmovel fp0,d0
  93.     fmoveml d1,fpcr
  94.         rts
  95. _PatchEnd:
  96. ");
  97.  
  98.  
  99. void CloseLibs(void)
  100. {
  101.    if(MathIeeeSingBasBase)
  102.       CloseLibrary(MathIeeeSingBasBase);
  103.  
  104.    if(MathIeeeDoubBasBase)
  105.       CloseLibrary(MathIeeeDoubBasBase);
  106. }
  107.  
  108.  
  109. int OpenLibs(void)
  110. {
  111.     MathIeeeSingBasBase = (struct MathIEEEBase *) OpenLibrary("mathieeesingbas.library",37);
  112.  
  113.     MathIeeeDoubBasBase = (struct MathIEEEBase *) OpenLibrary("mathieeedoubbas.library",37);
  114.  
  115.     if(MathIeeeSingBasBase && MathIeeeDoubBasBase)
  116.        return(2);
  117.     else if(MathIeeeSingBasBase)
  118.        return(1);
  119.     else
  120.     {
  121.        CloseLibs();
  122.        return(0);
  123.     }
  124. }
  125.  
  126.  
  127. int AllocSema(void)
  128. {
  129.    int ret=0;
  130.  
  131.    Forbid();
  132.    if(!FindSemaphore(SEMANAME))
  133.    {
  134.       sema = (struct sema_struct *) AllocMem(sizeof(struct sema_struct), MEMF_PUBLIC | MEMF_CLEAR);
  135.       if(sema)
  136.       {
  137.          sema->sm_sema.ss_Link.ln_Pri=-127;
  138.          sema->sm_sema.ss_Link.ln_Name=(APTR)&(sema->sm_name);
  139.          strcpy(sema->sm_sema.ss_Link.ln_Name,SEMANAME);
  140.  
  141.          AddSemaphore(sema); /* Does not work with KS < V36 */
  142.       }
  143.       else ret = 2;
  144.    }
  145.    else ret = 1;
  146.  
  147.    Permit();
  148.  
  149.    return(ret);
  150. }
  151.  
  152.  
  153. void FreeSema(void)
  154. {
  155.    if(sema)
  156.    {
  157.       RemSemaphore(sema);
  158.       ObtainSemaphore(sema);
  159.       ReleaseSemaphore(sema);
  160.       FreeMem(sema, sizeof(struct sema_struct));
  161.    }
  162. }
  163.  
  164.  
  165. int main(void)
  166. {
  167.    int err,openlibs;
  168.    APTR patch;
  169.    BPTR outp;
  170.  
  171.    outp = Output();
  172.    if(outp)
  173.    {
  174.       if(SysBase->AttnFlags & AFF_68040)
  175.       {
  176.          err = AllocSema();
  177.          if(err==0)
  178.          {
  179.             if(openlibs = OpenLibs())
  180.             {
  181.                patch = (APTR) AllocMem(PATCHSIZE, MEMF_PUBLIC);
  182.                if(patch)
  183.                {
  184.                   CopyMem(PatchBegin,patch,PATCHSIZE);
  185.                   CacheClearU();
  186.  
  187.                   err = SetFunction(MathIeeeSingBasBase,LVOIEEESPFix,patch+(ULONG)NewIEEESPFix-(ULONG)PatchBegin);
  188.                   err = SetFunction(MathIeeeSingBasBase,LVOIEEESPMul,patch+(ULONG)NewIEEESPMul-(ULONG)PatchBegin);
  189.                   err = SetFunction(MathIeeeSingBasBase,LVOIEEESPDiv,patch+(ULONG)NewIEEESPDiv-(ULONG)PatchBegin);
  190.                   if(openlibs==2)
  191.                      err = SetFunction(MathIeeeDoubBasBase,LVOIEEEDPFix,patch+(ULONG)NewIEEEDPFix-(ULONG)PatchBegin);
  192.  
  193.                   FPuts(outp,"SetMathPatch V1.00 (6.7.95) successfully installed. Patch list:\n");
  194.                   FPuts(outp,"\tIEEESPFix() patch\n");
  195.                   FPuts(outp,"\tIEEESPMul() patch\n");
  196.                   FPuts(outp,"\tIEEESPDiv() patch\n");
  197.                   if(openlibs==2)
  198.                      FPuts(outp,"\tIEEEDPFix() patch\n");
  199.                   else
  200.                      FPuts(outp,"'mathieeedoubbas.library' v37+ not found. IEEEDPFix() nopatched.\n");
  201.  
  202.                   /* No CloseLibrary !!! */
  203.  
  204.                   return(RETURN_OK);
  205.                }
  206.                else
  207.                {
  208.                   CloseLibs();
  209.                   FreeSema();
  210.  
  211.                   FPuts(outp,"Error: Not enough memory !\n");
  212.                   return(RETURN_FAIL);
  213.                }
  214.             }
  215.             else
  216.             {
  217.                FreeSema();
  218.  
  219.                FPuts(outp,"Can't open math-libraries\n");
  220.                return(RETURN_FAIL);
  221.             }
  222.  
  223.          } 
  224.          else if(err==1)
  225.          {
  226.             FPuts(outp,"SetMathPatch is already installed !\n");
  227.             return(RETURN_WARN);
  228.          }
  229.          else if(err==2)
  230.          {
  231.             FPuts(outp,"Error: Not enough memory !\n");
  232.             return(RETURN_FAIL);
  233.          }
  234.       }
  235.       else
  236.       {
  237.          FPuts(outp,"SetMathPatch is only needed for 68040 and greater !\n");
  238.          return(RETURN_WARN);
  239.       }
  240.    }
  241.    else
  242.       return(RETURN_FAIL);
  243. }
  244.